package main

import (
	"github.com/dgryski/trifles/uuid"
	"gqlt2/graph/generated"
	"gqlt2/graph/model"
	"gqlt2/graph/resolver"
	"log"
	"math/rand"
	"net/http"
	"os"
	"time"

	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
)

const defaultPort = "8080"

var locations []model.VehicleLocationLast

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = defaultPort
	}
	//initL()
	srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &resolver.Resolver{}}))

	http.Handle("/", playground.Handler("GraphQL playground", "/query"))
	http.Handle("/query", srv)
	http.Handle("/enterprise", srv)

	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

func initL() {
	locations = make([]model.VehicleLocationLast, 100000)
	for i := 0; i < 100000; i++ {

		location := model.VehicleLocationLast{
			Acceleration:        randownStr(),
			AlarmContent:        randownStr(),
			Alititude:           randownStr(),
			Direction:           randownStr(),
			TachographSpeed:     randomFloor(),
			IsLocate:            randomBoolean(),
			Coordinate:          randomPoint(),
			LocationDescription: randownStr(),
			Mileage:             randownStr(),
			GpsSpeed:            randomFloor(),
			StarCount:           randownStr(),
			StarStatus:          randownStr(),
			RoadName:            randownStr(),
			SpeedLimitThreshold: randomFloor(),
			CorrectCoordinate:   randomPoint(),
			Status:              randownStr(),
			Pid:                 randownStr(),
			Imei:                randownStr(),
			SimNumber:           randownStr(),
		}
		locations = append(locations, location)
	}
}

func randownStr() *string {
	r := uuid.UUIDv4()
	return &r
}

func randomFloor() *float64 {
	r := rand.Float64()
	return &r

}

func randomBoolean() *bool {
	r := true
	return &r
}

func currentTime() *time.Time {
	r := time.Now()
	return &r
}

func randomPoint() *model.Point {
	return &model.Point{
		Longitude: rand.Float64(),
		Latitude:  rand.Float64(),
	}
}
